﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using net.veekee.common.designpattern;

namespace net.veekee.common
{
    public class Log : Singleton<Log>
    {
        private String ConsoleSpinner = "/-\\-";
        private int ConsoleSpinnerIndx = 0;
        private StreamWriter logStream = null;
        private bool allowDebug = false;
        private bool allowTrace = false;

        public Log()
        {
            this.Reset();
        }

        public override void Dispose()
        {
            if (this.logStream != null)
            {
                this.logStream.Close();
                this.logStream.Dispose();
            }
        }

        public void Reset()
        {
            if (this.logStream != null)
                this.logStream.Close();
            String logfile = FS.ManageWorkingDir(Options.GetInstance().Get("log", null));
            this.allowDebug = Options.GetInstance().IsEnabled("debug");
            this.allowTrace = Options.GetInstance().IsEnabled("trace");
            if (logfile != null)
            {
                try
                {
                    FileInfo fi = new FileInfo(logfile);
                    if (!fi.Directory.Exists)
                        fi.Directory.Create();
                    this.logStream = new StreamWriter(logfile);
                }
                catch(Exception e)
                {
                    this.Error("Can't open log file: {0}", e.ToString());
                    this.logStream = null;
                }
            }
        }

        private void Write(String type, String msg, params Object[] args)
        {
            try
            {
                msg = String.Format(msg, args);
            }
            catch
            {
            }
            System.Diagnostics.Trace.WriteLine("\r" + msg);
            Console.WriteLine("\r" + msg);
            if (this.logStream != null)
            {
                this.logStream.WriteLine(DateTime.Now.ToString("G") + " - " + type + " - " + msg);
                this.logStream.Flush();
            }
        }

        public void WriteProgressBar(String msg, params Object[] args)
        {
            String prct = String.Format(msg, args);
            this.ConsoleSpinnerIndx = (this.ConsoleSpinnerIndx + 1) % 4;
            Console.Write("\r{0} {1}", prct, this.ConsoleSpinner[this.ConsoleSpinnerIndx]);
        }

        public void WriteProgressBarEnd()
        {
            //Console.WriteLine("");
        }

        public void Debug(String msg, params Object[] args)
        {
#if DEBUG
            this.allowDebug = true;
#endif
            if (this.allowDebug)
                this.Write("DEBUG", msg, args);
        }

        public void Trace(String msg, params Object[] args)
        {
            if (this.allowTrace)
                this.Write("TRACE", msg, args);
        }

        public void Info(String msg, params Object[] args)
        {
            this.Write("INFO ", msg, args);
        }

        public void Warn(String msg, params Object[] args)
        {
            this.Write("WARN ", msg, args);
        }

        public void Error(String msg, params Object[] args)
        {
            this.Write("ERROR", msg, args);
        }
    }
}
